package com.ruoyi.system.service.impl;

import java.util.*;

import com.ruoyi.common.utils.CharUtil;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysTokenMapper;
import com.ruoyi.system.domain.SysToken;
import com.ruoyi.system.service.ISysTokenService;
import com.ruoyi.common.core.text.Convert;

/**
 * token信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2019-12-05
 */
@Service
public class SysTokenServiceImpl implements ISysTokenService 
{
    @Autowired
    private SysTokenMapper sysTokenMapper;

    /**
     * 查询token信息
     * 
     * @param id token信息ID
     * @return token信息
     */
    @Override
    public SysToken selectSysTokenById(Long id)
    {
        return sysTokenMapper.selectSysTokenById(id);
    }

    @Override
    public SysToken selectSysTokenByUserId(Long userId) {
        return sysTokenMapper.selectSysTokenByUserId(userId);
    }

    @Override
    public SysToken selectSysTokenByLoginName(String loginName) {
        return sysTokenMapper.selectSysTokenByLoginName(loginName);
    }

    @Override
    public SysToken selectSysTokenByToken(String token) {
        return sysTokenMapper.selectSysTokenByToken(token);
    }

    /**
     * 查询token信息列表
     * 
     * @param sysToken token信息
     * @return token信息
     */
    @Override
    public List<SysToken> selectSysTokenList(SysToken sysToken)
    {
        return sysTokenMapper.selectSysTokenList(sysToken);
    }

    /**
     * 新增token信息
     * 
     * @param sysToken token信息
     * @return 结果
     */
    @Override
    public int insertSysToken(SysToken sysToken)
    {
        return sysTokenMapper.insertSysToken(sysToken);
    }

    /**
     * 修改token信息
     * 
     * @param sysToken token信息
     * @return 结果
     */
    @Override
    public int updateSysToken(SysToken sysToken)
    {
        sysToken.setUpdateTime(DateUtils.getNowDate());
        return sysTokenMapper.updateSysToken(sysToken);
    }

    /**
     * 删除token信息对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysTokenByIds(String ids)
    {
        return sysTokenMapper.deleteSysTokenByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除token信息信息
     * 
     * @param id token信息ID
     * @return 结果
     */
    @Override
    public int deleteSysTokenById(Long id)
    {
        return sysTokenMapper.deleteSysTokenById(id);
    }

    @Override
    public Map<String, Object> createToken(Long userId) {
        //生成一个token
        String token = CharUtil.getRandomString(32);
        //当前时间
        Date now = new Date();
        //过期时间
        //Date expireTime = new Date(now.getTime() + EXPIRE * 1000);

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, 30);
        Date expireTime = cal.getTime();

        //判断是否生成过token
        SysToken sysToken = sysTokenMapper.selectSysTokenByUserId(userId);
        if (sysToken == null) {
            sysToken = new SysToken();
            sysToken.setUserId(userId);
            sysToken.setToken(token);
            sysToken.setUpdateTime(now);
            sysToken.setExpireTime(expireTime);
            //保存token
            insertSysToken(sysToken);
        } else {
            sysToken.setToken(token);
            sysToken.setUpdateTime(now);
            sysToken.setExpireTime(expireTime);
            //更新token
            updateSysToken(sysToken);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("expireTime", expireTime);

        return map;
    }
}
